<template>
  <div class="product">
    <el-form ref="form" :data="form" :label-width="formLabelWidth">
      <el-form-item label="商品背景图">
        <el-upload
          class="avatar-uploader"
          action="/api/file/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="form.image_url" :src="form.image_url" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="跳转链接(商品)">
        <el-select v-model="form.product_id" clearable>
          <el-option v-for="(product, index) in productList" :key="index" :label="product.label" :value="product.value"/>
        </el-select>
      </el-form-item>

      <el-form-item label="是否设为banner">
        <el-switch v-model="form.is_banner"/>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存</el-button>
        <el-button @click="goBack">返回</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
import bannerApi from '@/api/banner.js'
import productApi from '@/api/product.js'
const defalultForm = {
  image_url: '',
  product_id: '',
  is_banner: false
}
export default {
  data() {
    return {
      formLabelWidth: 120 + 'px',
      form: Object.assign({}, defalultForm),
      productList: []
    }
  },
  computed: {
    isEdit() {
      return this.$route.meta.isEdit
    },
    id() {
      return this.$route.params.id
    }
  },
  created() {
    if(this.isEdit) {
      this.getBannerById()
    }
    this.getProducts()
  },
  methods: {
    // 获取商品
    async getProducts() {
      const query = {
        page: 0,
        limit: 9999
      }
      const result = await productApi.getProducts(query)
      let data = result.data.rows
      data.forEach(value => {
        let obj = {
          label: value.name,
          value: value.id
        }
        this.productList.push(obj)
      })
    },
    // 根据id获取banner
    async getBannerById() {
      const result = await bannerApi.getBannerById(this.id)
      this.form = result.data[0]
      if(result.data[0].is_banner == 1) {
        this.form.is_banner = true
      }else {
        this.form.is_banner = false
      }
    },
    // 添加或修改商品
    async onSubmit() {
      if(this.isEdit) {
        console.log(this.form)
        const result = await bannerApi.updateBanner(this.id, this.form)
        if(result.code===0) {
          this.$message.success('修改成功')
        }else {
          this.$message.warning('修改失败')
        }
      }else {
        console.log(this.form)
        const result = await bannerApi.addBanner(this.form)
        if(result.code===0) {
          this.$message.success('添加成功')
        }else {
          this.$message.warning('添加失败')
        }
      }
      this.goBack()
      this.reload()
    },
    // 返回
    goBack() {
      this.$router.go(-1)
    },
    handleAvatarSuccess(res, file) {
      console.log(res)
      this.form.image_url = res.filename;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  },
  inject: ['reload']
}
</script>
<style lang="less" scoped>
  .product {
    margin-top: 20px;
    .el-input {
      width: 300px;
    }
    .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
}
</style>
